Euler Problem 46

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

  • 9 = 7 + 2×12
  • 15 = 7 + 2×22
  • 21 = 3 + 2×32
  • 25 = 7 + 2×32
  • 27 = 19 + 2×22
  • 33 = 31 + 2×12

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?


In [1]:
N = 10000
sieve = [False, False] + [True]*N
for p in range(N):
    if sieve[p]:
        for m in range(p*p, N, p):
            sieve[m] = False
for n in range(9, N, 2):
    if sieve[n]:
        continue
    delta = 6
    m = n - 2
    while m > 2 and not sieve[m]:
        m -= delta
        delta += 4
    if m <= 2:
        print(n)
        break


5777

In [ ]: